package controller.api;

import java.util.ArrayList;
import java.util.List;
import java.util.Map.Entry;

import com.jfinal.core.Controller;
import com.jfinal.core.NotAction;
import com.jfinal.kit.Kv;
import com.jfinal.kit.Ret;
import com.jfinal.kit.StrKit;

import model.ServicerCompany;
import model.Staff;
import model.Station;
import model.Wxuser;

public class BaseApiController extends Controller{

	@NotAction
	public void renderOk(String err) {
		renderJson(Ret.ok().set("err" , err));
	}
	
	@NotAction
	public void renderOk() {
		renderJson(Ret.ok());
	}
	
	@NotAction
	public void renderFail(String err) {
		renderJson(Ret.fail().set("err" , err));
	}
	
	@NotAction
	public void renderFail() {
		renderJson(Ret.fail().set("err" , "系统异常，请联系管理员"));
	}
	
	protected Kv getParamKv() {
		Kv kv = Kv.create() ;
		for (Entry<String, String[]> entity : getParaMap().entrySet()) {
			if (StrKit.notBlank(entity.getValue()[0]) && !entity.getValue()[0].equals("null") ) {
				//多次传入的重复值只取第一个
				kv.set(entity.getKey(), entity.getValue()[0]);
			}
		}
		return kv ;
	}
	
	protected <T> List<T> getModelList(Class<T> modelClass,String modelName, int total){
        List<T> resultList = new ArrayList<T>();   
        for (int i = 0; i < total; i++) {
        	resultList.add(getModel(modelClass, modelName + i , true ));
		}
        return resultList ;
    }
	
	protected <T> List<T> getModelList(Class<T> modelClass){
		int total = getParaToInt("total");
        return this.getModelList(modelClass, "x" , total);
    }
	
	protected Wxuser getWxuser() {
		Wxuser wxuser = getAttr("wxuser");
		return wxuser ;
	}
	
	protected Station getStation() {
		Station station = getAttr("station") ;
		return station ;
	}
	
	protected Staff getStaff() {
		Staff Staff = getAttr("staff") ;
		return Staff ;
	}
	
	protected ServicerCompany getServicerCompany() {
		ServicerCompany servicerCompany = getAttr("servicerCompany");
		return servicerCompany ;
	}
}

